/**
 * Feishu Access 管理
 */
import Storage from '@/framework/cache/storage'
import DateUtil from '@/framework/utils/date-util'

const globalVal = globalThis as any;

// 访问信息缓存
const ACCESS_CACHE_KEY:string = (globalVal.APP_NAME || 'KWAISHOU') + '_ACCESS_DATA'


const FeishuManager = {
  // 获取访问数据
  _getAccessData: function () {
	  return Storage.get(ACCESS_CACHE_KEY)
  },
  get: function () {
    const accessData = FeishuManager._getAccessData()
    if (accessData && DateUtil.isExpired(accessData.expireAt)) {
      accessData.accessToken = null
      accessData.expireAt = null
    }
    return accessData
  },
  // 获取App ID
  getAppId: function () {
    const accessData = this.get()
    if (accessData) {
      return accessData.appId
    }
    return null
  },
  // 获取App Secret
  getAppSecret: function () {
    const accessData = this.get()
    if (accessData) {
      return accessData.appSecret
    }
    return null
  },
  // 设置accessData
  set: function (accessData) {
    Storage.set(ACCESS_CACHE_KEY, accessData)
  }
}

export default FeishuManager
